package com.trkj.service.lzs;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.trkj.dao.lzs.SalaryProjectDao;
import com.trkj.dao.lzs.SalarySableDao;
import com.trkj.pojo.ProjectTemplate;
import com.trkj.pojo.Recruitprocess;
import com.trkj.pojo.Salary;
import com.trkj.pojo.SalaryAccount;
import com.trkj.pojo.SalaryProject;
import com.trkj.service.ServiceException;

//新质表
@Service
@Transactional(rollbackFor = Exception.class)

public class SalarySableService {
	@Autowired
	private SalarySableDao salaryTemplateDao;
	@Autowired
	private SalaryProjectDao salaryProjectDao;
	
	  //查询所有
		public  List<ProjectTemplate>  SalarySablefindAll(){
		   return (List<ProjectTemplate>) salaryTemplateDao.findAll();
		}
		//添加
		public void wan(Timestamp templateCreatordate,String templateName,String templatePerson,BigDecimal varsionNumber) {
			try {
				ProjectTemplate ab= new ProjectTemplate();
				ab.setTemplateCreatordate(templateCreatordate);
				ab.setTemplateName(templateName);
				ab.setTemplatePerson(templatePerson);
				ab.setVarsionNumber(varsionNumber);
				salaryTemplateDao.save(ab);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		//根据姓名查找
		public  List<ProjectTemplate> queryByName (String  key){
			return salaryTemplateDao.queryByName("%" + key + "%");
			
		}
	    public   void lzs_Edit_a( long templateId ,Timestamp templateCreatordate,String templateName,String templatePerson, BigDecimal varsionNumber) {
	    	ProjectTemplate edit =salaryTemplateDao.findOne(templateId);
    	    edit.setTemplateCreatordate(templateCreatordate);
    	    edit.setTemplateName(templateName);
    	    edit.setTemplatePerson(templatePerson);
    	    edit.setVarsionNumber(varsionNumber);
    	   
    	    salaryTemplateDao.save(edit);
     }
     
     public ProjectTemplate chun_a(Long templateId) {
    	 return salaryTemplateDao.findOne(templateId);
 		
     }  
   //查询
		public ProjectTemplate  SalaryProjectfindone(long templateId){
			   return  salaryTemplateDao.findOne(templateId);
			}
		//删除
		 public boolean dltRa(Long templateId) {
		 		if(salaryTemplateDao.exists(templateId)){
		 			salaryTemplateDao.delete(templateId);
		 			 return true;
		 		}
		 		return false;		
	
		 }
		 
		 

		 
			/**
			 * 查询模板信息
			 * @param templateId
			 * @return
			 * @throws ServiceException
			 */
			public ProjectTemplate findOneTemplate(Long templateId) throws ServiceException {
				try {
					return salaryTemplateDao.findOne(templateId);
				} catch (Exception e) {
					e.printStackTrace();
					throw new ServiceException(e);
				}
			}
			
		//多表
			/**
			 * 模板关联薪资项目
			 * @param projectId
			 * @param templateId
			 * @throws ServiceException 
			 */
			public void RelevanceProject(Long[] projectId,Long templateId) throws ServiceException{
				try {
					ProjectTemplate projectTemplate = salaryTemplateDao.findOne(templateId);
					List<SalaryProject> list = new ArrayList<>();
					for(int i=0;i<projectId.length;i++){
						SalaryProject salaryProject = salaryProjectDao.findOne(projectId[i]);
						list.add(salaryProject);
						if(!salaryProject.getProjectTemplates().isEmpty()){
							salaryProject.getProjectTemplates().add(projectTemplate);
						}else{
							List<ProjectTemplate> lis=new ArrayList<>();
							lis.add(projectTemplate);
							salaryProject.setProjectTemplates(lis);
						}
					}
					projectTemplate.setSalaryProjects(list);
				} catch (Exception e) {
					e.printStackTrace();
					throw new ServiceException(e);
				}
			}
			
			/**
			 * 模板新增项目
			 * @param templateId
			 * @param salaryProject
			 * @param projectPerson
			 * @throws ServiceException 
			 */
			public void TemplateAddProject(Long templateId,SalaryProject salaryProject,String projectPerson) throws ServiceException{
				try {
					ProjectTemplate projectTemplate = salaryTemplateDao.findOne(templateId);	
					//新增项目
					salaryProject.setProjectPerson(projectPerson);
					salaryProject.setProjectCreatordate(new Timestamp(new Date().getTime()));
					salaryProject.setProjectUpdatedate(new Timestamp(new Date().getTime()));
					//项目添加模板
					List<ProjectTemplate> list=new ArrayList<>();
					list.add(projectTemplate);
					salaryProject.setProjectTemplates(list);
					salaryProjectDao.save(salaryProject);
					//模板添加项目
					if(!projectTemplate.getSalaryProjects().isEmpty()){
						projectTemplate.getSalaryProjects().add(salaryProject);
					}else{
						List<SalaryProject> list1=new ArrayList<>();
						list1.add(salaryProject);
						projectTemplate.setSalaryProjects(list1);
					}
				} catch (Exception e) {
					e.printStackTrace();
					throw new ServiceException(e);
				}
			}
			
			/**
			 * 查询模板关联的薪资项目
			 * @param projectId
			 * @param templateId
			 * @return
			 * @throws ServiceException 
			 */
			public List<SalaryProject> queryRelevanceProject(Long templateId) throws ServiceException{
				try {
					ProjectTemplate projectTemplate = salaryTemplateDao.findOne(templateId);
					List<SalaryProject> list = null;
					if(!projectTemplate.getSalaryProjects().isEmpty()){
						 list = projectTemplate.getSalaryProjects();	
					}
					/*for (SalaryProject salaryProject : list) {
					}*/
					return list;
				} catch (Exception e) {
					e.printStackTrace();
					throw new ServiceException(e);
				}
			}
		 
}
